package org.jeecgframework.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyInspectionPlanEntity;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @author xiul
 * 2016-4-1 下午6:30:43
 * Description 
 */
public class ZyProcessMainThread extends Thread {
	
	private String[] planIdsArr;
	private TSUser user;
	private String remarks;
	
	
	public ZyProcessMainThread(String[] planIdsArr,TSUser user,String remarks){
		this.planIdsArr = planIdsArr;
		this.user = user;
		this.remarks = remarks;
	}
	
	@Override
	public void run(){
		try {
			long be = System.currentTimeMillis();
			int maxUnit = 200;
			int count = planIdsArr.length;
			int threadC = count % maxUnit > 0 ? count / maxUnit + 1 : count / maxUnit;
			ThreadPoolExecutor exec = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadC);
			ArrayList<Future<Map<String, Object>>> results = new ArrayList<Future<Map<String, Object>>>();
			for (int i = 0; i < threadC; i++) {
				int beginR = i * maxUnit;
				int endR = i == (threadC -1 ) ? (count - 1) : beginR + maxUnit - 1 ;
				results.add(exec.submit(new ZyProcessThread(beginR,endR,planIdsArr,user,remarks)));
			}
			exec.shutdown();//线程执行完毕之后自动调用
			List<ZyInspectionPlanEntity> zyIps = new ArrayList<ZyInspectionPlanEntity>();
			while (true) {
				if(exec.isTerminated()){
					System.out.println("-->启动流程完毕");
					for (Future<Map<String, Object>> fs : results) {
						if(StringUtil.isNotEmpty(fs.get()) && StringUtil.isNotEmpty(fs.get().get("rtIps"))){
							zyIps.addAll(((List<ZyInspectionPlanEntity>)fs.get().get("rtIps")));
						}
					}
					break;
				}
				Thread.sleep(100);
			}
			count = zyIps.size();
//			threadC = count % maxUnit > 0 ? count / maxUnit + 1 : count / maxUnit;
			exec = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);//启动流程那里可以多线程并行，这里不能并行必须单线程跑
			exec.submit(new ZyProcessThreadSetpTwo(0, (count - 1), zyIps,user,remarks));
			exec.shutdown();
			while(true){
				if(exec.isTerminated()){
					System.out.println("--->>推动流程完毕");
					break;
				}
				Thread.sleep(100);
			}
			long en = System.currentTimeMillis();
			System.out.println("---->>>推动巡检流程完毕，用时："+(en - be) / 1000 +"." + (en - be) % 1000 +"秒");
		} catch (Exception e) {
			System.err.println("[启动巡检流程 线程] - 事务出现异常,回滚事务！");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//此处采用手动回滚
		}
	}
	
	public String[] getPlanIdsArr() {
		return planIdsArr;
	}
	public void setPlanIdsArr(String[] planIdsArr) {
		this.planIdsArr = planIdsArr;
	}

	public TSUser getUser() {
		return user;
	}
	public void setUser(TSUser user) {
		this.user = user;
	}

	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	
}
